package main

import (
	"fmt"
	"net"
	"runtime"

	"gitee.com/fhe-search/fhe-search-rpc/encrpc"
	"gitee.com/fhe-search/fhe-search-rpc/encsdk"
	"gitee.com/fhe-search/fhe-search-rpc/g"
	"gitee.com/fhe-search/fhe-search-rpc/utils/log"

	"google.golang.org/grpc"
)

func main() {
	os := runtime.GOOS
	switch os {
	case "windows":
		g.ReadConfig("cfg_win.toml")
	case "linux":
		g.ReadConfig("cfg_linux.toml")
	default:
		panic("本程序只支持 Windows 和 Linux")
	}
	log.InitLog(nil)
	defer log.CloseLog()

	encsdk.SetEncryptType(g.GetConfig().FheSearchConfig.EncryptType)
	encsdk.EncInit(g.GetConfig().FheSearchConfig.SecretKeyPath)
	// 开启端口
	listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", g.GetConfig().RpcConfig.Host, g.GetConfig().RpcConfig.Port))
	if err != nil {
		panic(err)
	}
	// 创建grpc服务端
	grpcServer := grpc.NewServer()
	// 注册服务
	encrpc.RegisterEncServiceServer(grpcServer, &encrpc.EncServer{})
	log.Log("encrypt rpc服务启动成功", fmt.Sprintf("%s:%d", g.GetConfig().RpcConfig.Host, g.GetConfig().RpcConfig.Port))
	// 启动服务
	err = grpcServer.Serve(listen)
	if err != nil {
		panic(err)
	}
}
